const store = {}

function resolve (pluginName) {
  if (pluginName in store) {
    return store[pluginName]
  } else {
    let plugin = null
    try {
      plugin = require(`../plugins/${pluginName}`)
    } catch (err) {
      throw new PluginError(`引入插件${pluginName}出错`)
    }
    if (typeof(plugin) !== 'function') {
      throw new PluginError(`插件模块必须是一个函数`)
    }
    store[pluginName] = plugin
    return plugin
  }
}

class PluginError extends Error {}

exports.PluginResolve = resolve
exports.PluginError = PluginError
